<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>自定义tabBar | Hexo</title><meta name="keywords" content="wx-小程序,tabBar,vant UI"><meta name="author" content="Joey"><meta name="copyright" content="Joey"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="tabBar的自定义配置前言： 按微信官方文档配置，搭配 vant UI 库的 tabBar样式， 可一键跳转至详细tabBar代码位置 点击 引入 vant UI安装vant1. 下载包12345678# 通过 npm 安装npm i @vant&#x2F;weapp -S --production# 通过 yarn 安装yarn add @vant&#x2F;weapp --production# 安装 0.x">
<meta property="og:type" content="article">
<meta property="og:title" content="自定义tabBar">
<meta property="og:url" content="https://jonhsean.gitee.io/blog/2022/08/25/daily/tabBar-wx/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="tabBar的自定义配置前言： 按微信官方文档配置，搭配 vant UI 库的 tabBar样式， 可一键跳转至详细tabBar代码位置 点击 引入 vant UI安装vant1. 下载包12345678# 通过 npm 安装npm i @vant&#x2F;weapp -S --production# 通过 yarn 安装yarn add @vant&#x2F;weapp --production# 安装 0.x">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2020/05/01/gkihqEjXxJ5UZ1C.jpg">
<meta property="article:published_time" content="2022-08-25T09:20:02.000Z">
<meta property="article:modified_time" content="2022-08-25T15:35:01.405Z">
<meta property="article:author" content="Joey">
<meta property="article:tag" content="wx-小程序">
<meta property="article:tag" content="tabBar">
<meta property="article:tag" content="vant UI">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2020/05/01/gkihqEjXxJ5UZ1C.jpg"><link rel="shortcut icon" href="/blog/img/favicon.png"><link rel="canonical" href="https://jonhsean.gitee.io/blog/2022/08/25/daily/tabBar-wx/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://unpkg.com/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://unpkg.com/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: undefined,
  localSearch: {"path":"/blog/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":100,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: {"limitDay":180,"position":"top","messagePrev":"It has been","messageNext":"days since the last update, the content of the article may be outdated."},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://unpkg.com/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://unpkg.com/flickr-justified-gallery/dist/fjGallery.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isAnchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '自定义tabBar',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-08-25 23:35:01'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">21</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">26</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">30</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/music/"><i class="fa-fw fas fa-music"></i><span> Music</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">Hexo</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/music/"><i class="fa-fw fas fa-music"></i><span> Music</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">自定义tabBar</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-08-25T09:20:02.000Z" title="发表于 2022-08-25 17:20:02">2022-08-25</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-08-25T15:35:01.405Z" title="更新于 2022-08-25 23:35:01">2022-08-25</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F/">微信小程序</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E8%87%AA%E5%AE%9A%E4%B9%89/">自定义</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/vant-UI/">vant UI</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/vant-UI/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F/">微信小程序</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">979</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>4分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="自定义tabBar"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><h1 id="tabBar的自定义配置"><a href="#tabBar的自定义配置" class="headerlink" title="tabBar的自定义配置"></a><strong>tabBar的自定义配置</strong></h1><p>前言： 按<a target="_blank" rel="noopener" href="https://developers.weixin.qq.com/miniprogram/dev/framework/ability/custom-tabbar.html">微信官方文档</a>配置，搭配 vant UI 库的 <a target="_blank" rel="noopener" href="https://vant-contrib.gitee.io/vant-weapp/#/tabbar">tabBar</a>样式，</p>
<p>可一键跳转至详细tabBar代码位置 <a href="./#3-%E7%BC%96%E5%86%99-tabBar-%E4%BB%A3%E7%A0%81">点击</a></p>
<h2 id="引入-vant-UI"><a href="#引入-vant-UI" class="headerlink" title="引入 vant UI"></a>引入 vant UI</h2><h3 id="安装vant"><a href="#安装vant" class="headerlink" title="安装vant"></a>安装vant</h3><h4 id="1-下载包"><a href="#1-下载包" class="headerlink" title="1. 下载包"></a>1. <a target="_blank" rel="noopener" href="https://vant-contrib.gitee.io/vant-weapp/#/quickstart#an-zhuang">下载包</a></h4><figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"># 通过 npm 安装</span><br><span class="line">npm i @vant/weapp -S --production</span><br><span class="line"></span><br><span class="line"># 通过 yarn 安装</span><br><span class="line">yarn add @vant/weapp --production</span><br><span class="line"></span><br><span class="line"># 安装 0.x 版本</span><br><span class="line">npm i vant-weapp -S --production</span><br></pre></td></tr></table></figure>

<h4 id="2-修改-app-json"><a href="#2-修改-app-json" class="headerlink" title="2. 修改 app.json"></a>2. 修改 <strong>app.json</strong></h4><p>将 <strong>app.json</strong> 中的 <code>&quot;style&quot;: &quot;v2&quot;</code> 去除</p>
<h4 id="3-修改-project-config-json"><a href="#3-修改-project-config-json" class="headerlink" title="3.  修改 project.config.json"></a>3.  修改 <strong>project.config.json</strong></h4><p>添加如下配置：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  ...</span><br><span class="line">  <span class="attr">&quot;setting&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">    ...</span><br><span class="line">    <span class="attr">&quot;packNpmManually&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span>  <span class="comment">// true</span></span><br><span class="line">    <span class="attr">&quot;packNpmRelationList&quot;</span><span class="punctuation">:</span> <span class="punctuation">[</span></span><br><span class="line">      <span class="punctuation">&#123;</span></span><br><span class="line">        <span class="attr">&quot;packageJsonPath&quot;</span><span class="punctuation">:</span> <span class="string">&quot;./package.json&quot;</span><span class="punctuation">,</span>  <span class="comment">//位置</span></span><br><span class="line">        <span class="attr">&quot;miniprogramNpmDistDir&quot;</span><span class="punctuation">:</span> <span class="string">&quot;./&quot;</span>	 	  <span class="comment">//位置</span></span><br><span class="line">      <span class="punctuation">&#125;</span></span><br><span class="line">    <span class="punctuation">]</span></span><br><span class="line">  <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

<h4 id="4-构建-npm-包"><a href="#4-构建-npm-包" class="headerlink" title="4. 构建 npm 包"></a>4. <strong>构建 npm 包</strong></h4><p>点击 <strong>工具 -&gt; 构建 npm</strong>，并勾选 <strong>使用 npm 模块</strong> 选项，构建完成后，即可引入组件。</p>
<hr>
<h3 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h3><h4 id="1-引入组件"><a href="#1-引入组件" class="headerlink" title="1. 引入组件"></a>1. 引入组件</h4><p>只需要在<code>app.json</code>或<code>index.json</code>中配置 Button 对应的路径即可</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 通过 npm 安装</span></span><br><span class="line"><span class="comment">// app.json  全局引入</span></span><br><span class="line"><span class="attr">&quot;usingComponents&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;van-button&quot;</span><span class="punctuation">:</span> <span class="string">&quot;@vant/weapp/button/index&quot;</span>  #引入btn</span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

<h4 id="2-使用组件-（在自定义tabBar的wxml中引入使用）"><a href="#2-使用组件-（在自定义tabBar的wxml中引入使用）" class="headerlink" title="2. 使用组件 （在自定义tabBar的wxml中引入使用）"></a>2. 使用组件 （在自定义tabBar的<code>wxml</code>中引入使用）</h4><blockquote>
<p><strong>注</strong>：该组件须配置微信 如下节所示</p>
</blockquote>
<p>在 wxml 中直接使用组件</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">van-button</span> <span class="attr">type</span>=<span class="string">&quot;primary&quot;</span>&gt;</span>按钮<span class="tag">&lt;/<span class="name">van-button</span>&gt;</span></span><br></pre></td></tr></table></figure>

<hr>
<h2 id="微信配置"><a href="#微信配置" class="headerlink" title="微信配置"></a>微信配置</h2><h3 id="1-配置信息"><a href="#1-配置信息" class="headerlink" title="1.  配置信息"></a>1.  配置信息</h3><ul>
<li><p><code>app.json</code> 中指定<code>tabBar</code> 项开启  <code>custom</code> 字段。同时其余 <code>tabBar</code> 相关配置也补充完整。</p>
</li>
<li><p>组件使用：</p>
<ul>
<li>局部：<strong>所有 tab 页的 json</strong> 里需声明 <code>usingComponents</code> 项，</li>
<li>全局注册： 也可以在 <code>app.json</code> 全局开启。</li>
</ul>
</li>
</ul>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">  <span class="attr">&quot;tabBar&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;custom&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span><span class="punctuation">,</span>  #<span class="number">1</span></span><br><span class="line">	...</span><br><span class="line">    <span class="attr">&quot;list&quot;</span><span class="punctuation">:</span> <span class="punctuation">[</span><span class="punctuation">&#123;</span>   </span><br><span class="line">      <span class="attr">&quot;pagePath&quot;</span><span class="punctuation">:</span> <span class="string">&quot;page/component/index&quot;</span><span class="punctuation">,</span>  #<span class="number">2</span>  必须与其tabBar对应页面</span><br><span class="line">      <span class="attr">&quot;text&quot;</span><span class="punctuation">:</span> <span class="string">&quot;组件&quot;</span></span><br><span class="line">    <span class="punctuation">&#125;</span><span class="punctuation">,</span> <span class="punctuation">&#123;</span></span><br><span class="line">      <span class="attr">&quot;pagePath&quot;</span><span class="punctuation">:</span> <span class="string">&quot;page/API/index&quot;</span><span class="punctuation">,</span></span><br><span class="line">      <span class="attr">&quot;text&quot;</span><span class="punctuation">:</span> <span class="string">&quot;接口&quot;</span></span><br><span class="line">    <span class="punctuation">&#125;</span><span class="punctuation">]</span></span><br><span class="line">  <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">  <span class="attr">&quot;usingComponents&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span><span class="punctuation">&#125;</span>  #可在这里开启 引入自定义组件(vant UI)</span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>



<h3 id="2-添加-tabBar-代码文件"><a href="#2-添加-tabBar-代码文件" class="headerlink" title="2. 添加 tabBar 代码文件"></a>2. 添加 tabBar 代码文件</h3><ul>
<li><p>在代码 <strong><code>根目录</code></strong> 下添加 <strong>入口</strong> 文件 </p>
<figure class="highlight text"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">custom-tab-bar/index.js</span><br><span class="line">custom-tab-bar/index.json</span><br><span class="line">custom-tab-bar/index.wxml</span><br><span class="line">custom-tab-bar/index.wxss</span><br></pre></td></tr></table></figure></li>
</ul>
<hr>
<h3 id="3-编写-tabBar-代码"><a href="#3-编写-tabBar-代码" class="headerlink" title="3. 编写 tabBar 代码"></a>3. 编写 tabBar 代码</h3><p><code>vant UI</code> <a target="_blank" rel="noopener" href="https://vant-contrib.gitee.io/vant-weapp/#/tabbar">tabBar</a></p>
<ul>
<li>引入组件(在<code>app.json</code>或<code>index.json</code>)</li>
</ul>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 在index.json中： </span></span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;usingComponents&quot;</span><span class="punctuation">:</span> <span class="punctuation">&#123;</span></span><br><span class="line">        <span class="attr">&quot;van-tabbar&quot;</span><span class="punctuation">:</span> <span class="string">&quot;@vant/weapp/tabbar/index&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;van-tabbar-item&quot;</span><span class="punctuation">:</span> <span class="string">&quot;@vant/weapp/tabbar-item/index&quot;</span><span class="punctuation">,</span></span><br><span class="line">        <span class="attr">&quot;van-icon&quot;</span><span class="punctuation">:</span> <span class="string">&quot;@vant/weapp/icon/index&quot;</span>  <span class="comment">//图标使用了icon组件 所以引入</span></span><br><span class="line">    <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;component&quot;</span><span class="punctuation">:</span> <span class="literal"><span class="keyword">true</span></span>  <span class="comment">//组件注册</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>wxml 使用</li>
</ul>
<blockquote>
<p><strong>Tabbar Props</strong>:</p>
<p>​        <code>active</code>  :当前选中标签的索引 , number类型</p>
<p><strong>Tabbar Event</strong>：</p>
<p>​        <code>bind:change </code> 切换标签时触发 ; <code>event.detail</code>: 当前选中标签的名称或索引值</p>
<p><strong>TabbarItem</strong>  :</p>
<p>​        <code>icon</code>  icon组件的图标名称或图片链接 ,  未选中时的图标</p>
<p>​        <code>icon-active</code>  选中时的图标</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">van-tabbar</span> <span class="attr">active</span>=<span class="string">&quot;&#123;&#123; active &#125;&#125;&quot;</span> <span class="attr">bind:change</span>=<span class="string">&quot;onChange&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">van-tabbar-item</span> <span class="attr">icon</span>=<span class="string">&quot;home-o&quot;</span>&gt;</span>标签<span class="tag">&lt;/<span class="name">van-tabbar-item</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">van-tabbar-item</span> <span class="attr">icon</span>=<span class="string">&quot;search&quot;</span>&gt;</span>标签<span class="tag">&lt;/<span class="name">van-tabbar-item</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">van-tabbar</span>&gt;</span></span><br><span class="line"></span><br><span class="line">使用循环的方式: </span><br><span class="line"><span class="tag">&lt;<span class="name">van-tabbar</span> <span class="attr">active</span>=<span class="string">&quot;&#123;&#123; active &#125;&#125;&quot;</span> <span class="attr">bind:change</span>=<span class="string">&quot;onChange&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">block</span> <span class="attr">wx:for</span>=<span class="string">&quot;&#123;&#123;arrBar&#125;&#125;&quot;</span> <span class="attr">wx:key</span>=<span class="string">&quot;*this&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">van-tabbar-item</span> <span class="attr">icon</span>=<span class="string">&quot;&#123;&#123;item.iconPath&#125;&#125;&quot;</span> </span></span><br><span class="line"><span class="tag">                     <span class="attr">icon-active</span>=<span class="string">&quot;&#123;&#123;item.selectedIconPath&#125;&#125;&quot;</span>&gt;</span></span><br><span class="line">        &#123;&#123;item.text&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">van-tabbar-item</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">block</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">van-tabbar</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ul>
<li>js 配置</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Component</span>(&#123;</span><br><span class="line">  <span class="attr">data</span>: &#123;</span><br><span class="line">    <span class="attr">active</span>: <span class="number">0</span>,</span><br><span class="line">    <span class="attr">arrBar</span>: [&#123;</span><br><span class="line">        <span class="string">&quot;pagePath&quot;</span>: <span class="string">&quot;/pages/home/home&quot;</span>,</span><br><span class="line">        <span class="string">&quot;text&quot;</span>: <span class="string">&quot;首页&quot;</span>,</span><br><span class="line">        <span class="string">&quot;iconPath&quot;</span>: <span class="string">&quot;star-o&quot;</span>,</span><br><span class="line">        <span class="string">&quot;selectedIconPath&quot;</span>: <span class="string">&quot;star&quot;</span></span><br><span class="line">      &#125;,&#123;</span><br><span class="line">        <span class="string">&quot;pagePath&quot;</span>: <span class="string">&quot;/pages/menu/menu&quot;</span>,</span><br><span class="line">        <span class="string">&quot;text&quot;</span>: <span class="string">&quot;菜单&quot;</span>,</span><br><span class="line">        <span class="string">&quot;iconPath&quot;</span>: <span class="string">&quot;phone-o&quot;</span>,</span><br><span class="line">        <span class="string">&quot;selectedIconPath&quot;</span>: <span class="string">&quot;phone&quot;</span></span><br><span class="line">      &#125;]</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">methods</span>:&#123;</span><br><span class="line">    <span class="title function_">onChange</span>(<span class="params">event</span>) &#123; <span class="comment">// event.detail 的值为当前选中项的索引</span></span><br><span class="line">        <span class="keyword">let</span> index = event.<span class="property">detail</span></span><br><span class="line">    	<span class="variable language_">this</span>.<span class="title function_">setData</span>(&#123; <span class="attr">active</span>: event.<span class="property">detail</span> &#125;);</span><br><span class="line">        </span><br><span class="line">       wx.<span class="title function_">switchTab</span>(&#123;</span><br><span class="line">          <span class="attr">url</span>: <span class="variable language_">this</span>.<span class="property">data</span>.<span class="property">arrBar</span>[index].<span class="property">pagePath</span>,</span><br><span class="line">       &#125;)</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>



<h3 id="4-在每个tabBar对应页面-绑定上索引值"><a href="#4-在每个tabBar对应页面-绑定上索引值" class="headerlink" title="4. 在每个tabBar对应页面 绑定上索引值"></a>4. 在每个tabBar对应页面 绑定上索引值</h3><p>不设置会发生错乱，显示页面与点击跳转的目标页面不符合</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"># 第一个页面home的<span class="variable constant_">JS</span>文件： </span><br><span class="line"><span class="title class_">Page</span>(&#123;</span><br><span class="line">    <span class="title function_">onShow</span>(<span class="params"></span>) &#123;</span><br><span class="line">      <span class="variable language_">this</span>.<span class="title function_">getTabBar</span>().<span class="title function_">setData</span>(&#123;</span><br><span class="line">        <span class="attr">active</span>:<span class="number">0</span></span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"># 其他页面同样设置,active值与其索引对应...</span><br></pre></td></tr></table></figure>



<p>-大功告成~</p>
<hr>
<p>参考：</p>
<ul>
<li>B站 - <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1VU4y157nX">小程序+Weapp自定义TabBar</a> </li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://jonhsean.gitee.io/blog">Joey</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://jonhsean.gitee.io/blog/2022/08/25/daily/tabBar-wx/">https://jonhsean.gitee.io/blog/2022/08/25/daily/tabBar-wx/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://jonhsean.gitee.io/blog" target="_blank">Hexo</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/wx-%E5%B0%8F%E7%A8%8B%E5%BA%8F/">wx-小程序</a><a class="post-meta__tags" href="/blog/tags/tabBar/">tabBar</a><a class="post-meta__tags" href="/blog/tags/vant-UI/">vant UI</a></div><div class="post_share"><div class="social-share" data-image="https://i.loli.net/2020/05/01/gkihqEjXxJ5UZ1C.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://unpkg.com/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://unpkg.com/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2022/08/21/daily/hexo%E9%85%8D%E7%BD%AE/"><img class="prev-cover" src="https://i.loli.net/2020/05/01/gkihqEjXxJ5UZ1C.jpg" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">0823</div></div></a></div><div class="next-post pull-right"><a href="/blog/2022/08/25/daily/%E5%8C%85%E7%AE%A1%E7%90%86/"><img class="next-cover" src="https://i.loli.net/2020/05/01/gkihqEjXxJ5UZ1C.jpg" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">npm参数</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#tabBar%E7%9A%84%E8%87%AA%E5%AE%9A%E4%B9%89%E9%85%8D%E7%BD%AE"><span class="toc-text">tabBar的自定义配置</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%95%E5%85%A5-vant-UI"><span class="toc-text">引入 vant UI</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85vant"><span class="toc-text">安装vant</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E4%B8%8B%E8%BD%BD%E5%8C%85"><span class="toc-text">1. 下载包</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E4%BF%AE%E6%94%B9-app-json"><span class="toc-text">2. 修改 app.json</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E4%BF%AE%E6%94%B9-project-config-json"><span class="toc-text">3.  修改 project.config.json</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-%E6%9E%84%E5%BB%BA-npm-%E5%8C%85"><span class="toc-text">4. 构建 npm 包</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8"><span class="toc-text">使用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E5%BC%95%E5%85%A5%E7%BB%84%E4%BB%B6"><span class="toc-text">1. 引入组件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E4%BD%BF%E7%94%A8%E7%BB%84%E4%BB%B6-%EF%BC%88%E5%9C%A8%E8%87%AA%E5%AE%9A%E4%B9%89tabBar%E7%9A%84wxml%E4%B8%AD%E5%BC%95%E5%85%A5%E4%BD%BF%E7%94%A8%EF%BC%89"><span class="toc-text">2. 使用组件 （在自定义tabBar的wxml中引入使用）</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BE%AE%E4%BF%A1%E9%85%8D%E7%BD%AE"><span class="toc-text">微信配置</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E9%85%8D%E7%BD%AE%E4%BF%A1%E6%81%AF"><span class="toc-text">1.  配置信息</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E6%B7%BB%E5%8A%A0-tabBar-%E4%BB%A3%E7%A0%81%E6%96%87%E4%BB%B6"><span class="toc-text">2. 添加 tabBar 代码文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E7%BC%96%E5%86%99-tabBar-%E4%BB%A3%E7%A0%81"><span class="toc-text">3. 编写 tabBar 代码</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E5%9C%A8%E6%AF%8F%E4%B8%AAtabBar%E5%AF%B9%E5%BA%94%E9%A1%B5%E9%9D%A2-%E7%BB%91%E5%AE%9A%E4%B8%8A%E7%B4%A2%E5%BC%95%E5%80%BC"><span class="toc-text">4. 在每个tabBar对应页面 绑定上索引值</span></a></li></ol></li></ol></li></ol></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 By Joey</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="/blog/js/tw_cn.js"></script><script src="https://unpkg.com/@fancyapps/ui/dist/fancybox.umd.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://unpkg.com/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script src="/blog/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://unpkg.com/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script src="https://unpkg.com/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = true;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://unpkg.com/butterfly-extsrc/dist/click-heart.min.js" async="async" mobile="true"></script><script src="https://unpkg.com/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"]):not([href="/music/"]):not([href="/no-pjax/"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>